package com.roc.rocketmqconsumer.messageModel;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * 点对点模型-消费者-2
 *
 * @author Roker
 * @date 2025/07/09
 */
public class PointToPointGroupAConsumer2 {
    public static void main(String[] args) throws Exception {
        // 1、创建一个消费者实例，并指定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("roc-consumer-group-A");
        // 2、设置NameServer地址
        consumer.setNamesrvAddr("localhost:9876");
        // 3、订阅主题和标签
        consumer.subscribe("TOPIC_POINT_TO_POINT", "*");

        // 4、注册消息监听器，处理接收到的消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    System.out.println("消费者组-A-消费者-2,接受到消息: " + new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        // 5、启动消费者实例
        consumer.start();
        System.out.println("点对点消费者组-A-消费者-2 启动，等待接收消息……");
    }
}